package com.ruoyi.project.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.Project;
import com.ruoyi.project.service.IProjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目管理Controller
 *
 * @author Zul
 * @date 2020-11-20
 */
@Controller
@RequestMapping("/res/project")
public class ProjectController extends BaseController {
    private String prefix = "res";

    @Autowired
    private IProjectService projectService;

    @RequiresPermissions("res:project:view")
    @GetMapping()
    public String project() {
        return String.format("%s/project", prefix);
    }

    /**
     * 查询项目管理列表
     */
    @RequiresPermissions("res:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project) {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @RequiresPermissions("res:project:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Project project) {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 新增项目管理
     */
    @GetMapping("/add")
    public String add() {
        return String.format("%s/add", prefix);
    }

    /**
     * 新增保存项目管理
     */
    @RequiresPermissions("res:project:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Project project) {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目管理
     */
    @GetMapping(value = {"/edit/{id}", "/edit/"})
    public String edit(@PathVariable(value = "id",required = false) Long id, ModelMap mmap) {
        Project project = null;
        if (StringUtils.isNull(id)) {
            project = new Project();
        } else {
            project = projectService.selectProjectById(id);
        }
        mmap.put("project", project);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目管理
     */
    @RequiresPermissions("res:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Project project) {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目管理
     */
    @RequiresPermissions("res:project:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectService.deleteProjectByIds(ids));
    }
    
    @RequestMapping("getProject")
    @ResponseBody
    public TableDataInfo getProject(){
        List<SysDictData> dataList = projectService.selectProjectToDict();
        return getDataTable(dataList);
    }
}
